package android.moodle;

import android.Graphics.GameRenderer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.javaClasses.DataHelper;
import android.javaClasses.Game;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by IntelliJ IDEA.
 * User: michelle
 * Date: 5/1/11
 * Time: 11:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class GameOnPhoneActivity extends Activity {
    private GameRenderer renderer;
    private GLSurfaceView glView;
    private TextView gameHeader;
    private int typeOfGame;
    private int numPlayers;
    private Context context;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamescreen);
        context = this;

        Bundle extras = getIntent().getExtras();
        numPlayers = extras.getInt("numPlayers");
        typeOfGame = extras.getInt("typeOfGame");

        final Vibrator v = (Vibrator) getSystemService( Context.VIBRATOR_SERVICE );

        // Get the width and height of the screen, so it's scalable
        Display display = getWindowManager().getDefaultDisplay();
        float width = display.getWidth();

        // Subtracts 25 pixels because that is the height of the statusbar
        float height = display.getHeight() - 25;

        //Declares the GLSurfaceView that is used for drawing the game
        glView = (GLSurfaceView) findViewById(R.id.glsurfaceview);

        //Declares the Gamerenderer that is used to draw the game
        renderer = new GameRenderer( this, width, height, typeOfGame, numPlayers);
        glView.setRenderer( renderer );

        gameHeader = (TextView) findViewById(R.id.gameheader);

        glView.setOnTouchListener( new View.OnTouchListener()
        {
            public boolean onTouch( View view, MotionEvent motionEvent )
            {
                v.vibrate(100);
                float xValue = motionEvent.getX();
                float yValue = motionEvent.getY();
                renderer.drawSymbol(xValue, yValue);
                if(renderer.getPlayerWon()) {
                    //Game theGame = new Game(players, winner);
                    DataHelper helper = new DataHelper(context);
                    //helper.insert(theGame);
                    startActivity(new Intent(context, ScoresActivity.class));


                }
                gameHeader.setText( renderer.getHeader() );
                return false;
            }
        });
    }
}